
package com.lucky.back.service.impl;

import com.lucky.back.ex.ServiceException;


import com.lucky.back.mapper.MessagesMapper;
import com.lucky.back.pojo.dto.MessagesAddNewDTO;
import com.lucky.back.pojo.entity.Messages;
import com.lucky.back.pojo.vo.MessagesListItemVO;
import com.lucky.back.pojo.vo.MessagesStandardVO;
import com.lucky.back.service.IMessagesService;
import com.lucky.back.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
public class MessagesServiceImpl implements IMessagesService {
    @Autowired
    MessagesMapper messagesMapper;


    @Override
    public void addNew(MessagesAddNewDTO messagesDTO) {

        Messages messages = new Messages();
        BeanUtils.copyProperties(messagesDTO,messages);
        messagesMapper.insert(messages);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理剩删除留言的请求.参数={}",id);
        MessagesStandardVO queryResult = messagesMapper.getStandardById(id);
        if (queryResult ==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"删除留言失败,删除的数据不存在");
        }
        log.debug("即将删除出数据");
        messagesMapper.deleteById(id);
        log.debug("删除完成!");
    }

    @Override
    public List<MessagesListItemVO> list() {
        log.debug("开始处理【查询留言列表】的业务");
        List<MessagesListItemVO> list = messagesMapper.list();
        return list;
    }
}

